package com.mianshi.facejava.modules.facejava.controller;

import java.text.SimpleDateFormat;
import java.util.*;

import com.mianshi.facejava.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.mianshi.facejava.modules.facejava.entity.JavaQuestionEntity;
import com.mianshi.facejava.modules.facejava.service.JavaQuestionService;
import com.mianshi.facejava.common.utils.PageUtils;
import com.mianshi.facejava.common.utils.R;


/**
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-07-12 16:28:53
 */
@RestController
@RequestMapping("facejava/javaquestion")
public class JavaQuestionController {
    @Autowired
    private JavaQuestionService javaQuestionService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("facejava:javaquestion:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = javaQuestionService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/query4wx")
    public R query4wx() {
        List<JavaQuestionEntity> questions = javaQuestionService.selectList(null);
        List<Map<String, Object>> result = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (JavaQuestionEntity question : questions) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", question.getId());
            map.put("title", question.getQuestion());
            map.put("date", sdf.format(question.getCreateTime()));
            map.put("description", question.getAnswer().length() > 200 ? question.getAnswer().substring(0, 200) : question.getAnswer());
            result.add(map);
        }
        return R.ok().put("content", result);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("facejava:javaquestion:info")
    public R info(@PathVariable("id") Long id) {
        JavaQuestionEntity javaQuestion = javaQuestionService.selectById(id);

        return R.ok().put("javaQuestion", javaQuestion);
    }

    @RequestMapping("/get")
    public R get(Long id) {
        JavaQuestionEntity javaQuestion = javaQuestionService.selectById(id);
        return R.ok().put("content", javaQuestion.getAnswer());
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("facejava:javaquestion:save")
    public R save(@RequestBody JavaQuestionEntity javaQuestion) {
        javaQuestionService.insert(javaQuestion);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("facejava:javaquestion:update")
    public R update(@RequestBody JavaQuestionEntity javaQuestion) {
        ValidatorUtils.validateEntity(javaQuestion);
        javaQuestionService.updateAllColumnById(javaQuestion);//全部更新

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("facejava:javaquestion:delete")
    public R delete(@RequestBody Long[] ids) {
        javaQuestionService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/level")
    @RequiresPermissions("facejava:javaquestion:level")
    public R level(@RequestBody Long[] ids, @RequestParam Integer level) {
        for (Long id : ids) {
            JavaQuestionEntity javaQuestionEntity = new JavaQuestionEntity();
            javaQuestionEntity.setId(id);
            javaQuestionEntity.setLevel(level);
            javaQuestionService.updateById(javaQuestionEntity);
        }
        return R.ok();
    }

}
